# -*- coding:utf-8 -*-

from bs4 import BeautifulSoup
from ping import verbose_ping
import requests


def get_proxy(protocol_input, url='http://www.xicidaili.com/nn'):
    header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
    }
    response = requests.get(url=url, headers=header)
    bs = BeautifulSoup(response.text, 'lxml')
    ip_list = bs.find(attrs={'id': 'ip_list'}).find_all('tr')
    candidate_ip = []
    for ip_i in ip_list[1:]:
        ip_content = ip_i.find_all('td')
        ip = ip_content[1].text
        port = ip_content[2].text
        protocol = ip_content[5].text
        time = ip_content[-2].text
        if protocol == protocol_input:
            if u'天' in time:
                candidate_ip.append((ip, port))
                # print ip, port, protocol, time
            elif u'小时' in time:
                judge_time = int(time.replace(u'小时', ''))
                if judge_time > 10:
                    candidate_ip.append((ip, port))
                    # print ip, port, protocol, time
    candidate_ip_delay = []
    for i in candidate_ip:
        ping_interval = verbose_ping(i[0], '192.168.69.202')
        print (i[0], i[1], ping_interval)
        candidate_ip_delay.append((i[0], i[1], ping_interval))
    candidate_ip_delay_sorted = sorted(candidate_ip_delay, key=lambda item: item[2])
    print candidate_ip_delay_sorted
    return candidate_ip_delay_sorted


if __name__ == '__main__':
    print get_proxy("HTTP")
